<?php
/***
	联系记录表，从客户列表的链接过来。
	只包括一个方法：
	index($client_id)，需要输入客户ID
	输入方面：
	1. 接收由客户列表页面的链接传入的$client_id；
	2. 接收新建表单传入的POST数据；
	3. 接收更新表单传入的POST数据；
	
**/
class Contact_record extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->model('contact_record_model');
		$this->load->model('client_model');
		$this->load->model('contact_person_model');
		$this->load->helper('url');
	}
	//显示添加联系人表单
	public function index($client_id = 0, $id = 0) {
		//查寻client_name
		
		if ($client_id <> 0) {
			$client = $this->client_model->get_by_id($client_id);
			$client_name = $client['name'];
		}
			
		//新建表单的提交
		if (isset($_POST['form1'])) {
			$client_id = $_POST['client_id'];
			$client_name = $_POST['client_name'];
			$this->contact_record_model->create(); //同时更新clients表中的record1 & record2
		}
		
		//更新表单的提交
		if (isset($_POST['form2'])) {
			$client_id = $_POST['client_id'];
			$client_name = $_POST['client_name'];
			$id = $_POST['id'];
			//需要记录下更新的时间
			$now = date('Y-m-d');
			
			$this->contact_record_model->update_all($id, $now); //同时更新clients表中的record1 & record2
			$id = 0; //更新完毕，之后还是加载添加联系信息表单
		}
		
		//查询对应客户的所有联系人，返回其id和name用于传递给表单使用
		//在这个位置，不论何时都应该有了$client_id
		$data['persons'] = $this->contact_person_model->get_by_client_all($client_id);
		
		$data ['records'] = $this->contact_record_model->get_by_client($client_id);
		
		$data['title'] = '联系表';
		$data['client_id'] = $client_id;
		$data['client_name'] = $client_name;
		
		//加载模版解析类
		$this->load->library('parser');
		
		$this->load->view('templates/head');
		$this->load->view('templates/menu');
		//添加联系信息表单
		if ($id == 0) {
			$this->parser->parse('contact_record/form_create', $data);
		}
		//更新联系信息表单
		if ($id <> 0) {
			$data['id'] = $id; //放到表单，再传回用来定位更新那条记录。
			//去查这个id的记录
			$record = $this->contact_record_model->get_by_id($id);
			//把这些分配给$data
			$data['cp_id'] = $record['cp_id'];
			$data['means'] = $record['means'];
			$data['contact_date'] = $record['contact_date'];
			$data['status'] = $record['status'];
			$data['remark'] = $record['remark'];
			
			//cp_id要查到名字
			$cp_record = $this->contact_person_model->get_by_id($data['cp_id']);
			$data['cp_name'] = $cp_record['name'];
			
			$this->parser->parse('contact_record/form_edit', $data);
		}
		
		
		//所选客户联系人列表
		$this->parser->parse('contact_record/list', $data);
		$this->load->view('templates/foot');
	}
	
	//按月分，按人过滤显示工作量（最好过滤设置在视图上）
	public function work_load( ) {
		//搜索所有记录
		$records = $this->contact_record_model->get_all( );
		$data['records'] = $records;
		//加载模版解析类
		$this->load->library('parser');
		
		$this->load->view('templates/head');
		$this->load->view('templates/menu');
		$this->parser->parse('contact_record/work_load', $data);
		$this->load->view('templates/foot');
	}

	public function del($client_id, $id = 0) {
		//调用模块去删除这个记录
		$this->contact_record_model->del_by_id($id);
		
		$data['persons'] = $this->contact_person_model->get_by_client($client_id);
		$data ['records'] = $this->contact_record_model->get_by_client($client_id);
		$client = $this->client_model->get_by_id($client_id);
		$client_name = $client['name'];
		
		$data['title'] = '联系表';
		$data['client_id'] = $client_id;
		$data['client_name'] = $client_name;
		
		//加载模版解析类
		$this->load->library('parser');
		$this->load->view('templates/head');
		$this->load->view('templates/menu');
		$this->parser->parse('contact_record/form_create', $data);
		$this->parser->parse('contact_record/list', $data);
		$this->load->view('templates/foot');
	
	}
	
	//导出CSV文件
	public function export_csv( ) {
	//去数据库整合数据，按原来的表格
	$raw_data = $this->contact_record_model->get_all_csv();
	$data = array();
	//合并相同公司，相同公司的联系人、联系记录将追加到上一条记录里面,$i将不变；
	//
	//所有的字段:means, contact_date, status, contact_name, contact_mobile, contact_tel, contact_email, contact_id, contact_position, client_name, client_id, country, contact_by, website, address, client_type, scope
	$i = 0;
	$client_id = 0;
	$contact_id = 0;
	$data = array();
	foreach ($raw_data as $data_item) {
		if ($client_id == $data_item['client_id'] and $data_item['client_id'] <> 0) {
			if ($contact_id == $data_item['contact_id']) {
				//增加一个记录
				//如果这是一条空记录那就不要增加记录
				if ($data_item['contact_date']) {
					$data[$i-1]['record'] = $data[$i-1]['record']."\n".$data_item['contact_name'].", ".$data_item['means'].", ".$data_item['contact_date'].", ".$data_item['status'];
				}
			} else {
				//增加联系人和一个记录
				//如果这个联系人是Others，那就不要增加联系人
				//如果这是一条空记录那就不要增加记录
				if ($data_item['contact_name'] <> 'Others') {
					$data[$i-1]['contact'] = $data[$i-1]['contact']."\n".$data_item['contact_name']."/".$data_item['contact_position']." M: ".$data_item['contact_mobile']." P:".$data_item['contact_tel']." E:".$data_item['contact_email'];
				}
				if ($data_item['contact_date']) {
					$data[$i-1]['record'] = $data[$i-1]['record']."\n".$data_item['contact_name'].", ".$data_item['means'].", ".$data_item['contact_date'].", ".$data_item['status'];
				}
			}
		} else {
			$client_id = $data_item['client_id'];
			$contact_id = $data_item['contact_id'];
			
			$data[]=array (
				'contact_by' => $data_item['contact_by'],
				'country' => $data_item['country'],
				'client_name' => $data_item['client_name'],
				'website' => $data_item['website'],
				'address' => $data_item['address'],
				'client_type' => $data_item['client_type'],
				'scope' => $data_item['scope'],
				'contact' => $data_item['contact_name']."/".$data_item['contact_position']." M: ".$data_item['contact_mobile']." P:".$data_item['contact_tel']." E:".$data_item['contact_email'],
				'record' => $data_item['contact_name'].", ".$data_item['means'].", ".$data_item['contact_date'].", ".$data_item['status']
				);
			$i = $i + 1;
		
		}
		
		
	}
	
	
	
	//加载下载辅助函数
	$this->load->helper('download');
	//删除文件
	unlink("./temp/client.csv");
	//打开文件
	@$fp=fopen("./temp/client.csv", "a+");
	
	//写入数据
	
	$head = array ("负责人", "国家", "客户名", "网址", "地址", "客户类别", "服务范围", "联系方式", "联系记录");
	foreach ($head as $i => $v) {
		$head[$i] = iconv('utf-8', 'gbk', $v);
	}
	fputcsv($fp, $head);
	
	foreach ($data as $data_item){
		$mystring = array( $data_item['contact_by'], $data_item['country'], $data_item['client_name'], $data_item['website'], $data_item['address'], $data_item['client_type'], $data_item['scope'], $data_item['contact'], $data_item['record']);
		foreach ($mystring as $i => $v) {
		$mystring[$i] = iconv('utf-8', 'gbk', $v);
		}
		
		fputcsv($fp, $mystring);
	}
	
	
	
	//关闭文件
	fclose($fp);
	
	//下载这个文件
	force_download('./temp/client.csv', NULL);
	}
}